package com.aitemv.services.sqlMap;

import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.StringUtils;

import com.aitemv.base.BaseEntity;
import com.aitemv.utils.CommUtil;
/**
 * @ClassName: BaseSqlMap 
 * @Description: BaseSqlMap,所有sqlMap的基类
 * @author java_lyvee@126.com 
 * @date 2014-1-9 下午7:34:45 
 */
public abstract class BaseSqlMap {
	/**
	 * sql语句中查询的所有字段
	 */
	protected String sqlField;
	
	/**
	 * sql的表名
	 */
	protected String sqlTableName;
	
	public StringBuffer queryForListSql;
	
	
	/**
	 * @Title: buildSqlField 
	 * @Description: 拼装sql语句中所有要查询的字段
	 * @param entity
	 * @return void  返回类型
	 */
	protected void buildSqlField(BaseEntity entity){
		sqlField = CommUtil.conditSqlFieldByEntity(entity);
	}
	
	/**
	 * @Title: buildSqlTableName 
	 * @Description: 获得sql的表名
	 * @param entity  
	 * @return void  返回类型
	 * @throws
	 */
	protected void buildSqlTableName(BaseEntity entity){
		sqlTableName = entity.getClass().getSimpleName().toLowerCase().replaceAll("entity", "");
	}
	
	/**
	 * @Title: buildSqlTableName 
	 * @Description: 获得sql的表面
	 * @param entity  
	 * @return void  返回类型
	 * @throws
	 */
	protected void buildSqlTableNames(Set<BaseEntity> entity){
		//TODO
	}
	
	
	/**
	 * @Title: conditionSql 
	 * @Description: 拼装sql
	 * @param entity
	 * @return  queryForListSql
	 * @return StringBuffer  返回类型
	 */
	public  StringBuffer conditionSql(BaseEntity entity) {
		queryForListSql = new StringBuffer("SELECT ");
		this.buildSqlField(entity);
		queryForListSql.append(sqlField+" FROM ");
		this.buildSqlTableName(entity);
		queryForListSql.append(sqlTableName);
		return queryForListSql;
	}
	
	/**
	 * 
	 * @Title: conditionSql 
	 * @Description: 拼装sql带where
	 * @param entity
	 * @param map
	 * @return StringBuffer  返回类型
	 */
	public StringBuffer conditionSql(BaseEntity entity,Map<String, Object> map) {
		queryForListSql = new StringBuffer("SELECT ");
		this.buildSqlField(entity);
		queryForListSql.append(sqlField+" FROM ");
		this.buildSqlTableName(entity);
		queryForListSql.append(sqlTableName);
		if(map!=null&&map.size()>0){
			queryForListSql.append(" where ");
			for(String key :map.keySet()){
				queryForListSql.append(" "+key+" =? ");
			}
		}else {
			return queryForListSql;
		}
		return queryForListSql;
	}
	
	/**
	 * @Title: conditionSql 
	 * @Description: 拼装sql语句，conut查询共有多少记录的sql
	 * @param entity
	 * @param map
	 * @param propertyName
	 * @return  queryForListSql
	 * @return StringBuffer  返回类型
	 */
	public StringBuffer conditionSql(BaseEntity entity,Map<String, Object> map,String propertyName) {
		queryForListSql = new StringBuffer("SELECT ");
		if(StringUtils.isNotBlank(propertyName)){
			queryForListSql.append("count("+propertyName+") total "+" FROM ");
		}else{
			queryForListSql.append("count(id) total "+" FROM ");
		}
		this.buildSqlTableName(entity);
		queryForListSql.append(sqlTableName);
		if(map!=null&&map.size()>0){
			queryForListSql.append(" where ");
			for(String key :map.keySet()){
				queryForListSql.append(" "+key+" =? ");
			}
		}else {
			return queryForListSql;
		}
		return queryForListSql;
	}
	
	/**
	 * @Title: conditionAddOrUpdateSql 
	 * @Description: 拼装添加的sql语句
	 * @param entity
	 * @return sql 
	 * @return String  返回类型
	 * @throws
	 */
	public  String conditionAddOrUpdateSql(BaseEntity entity) {
		List<String> list = CommUtil.conditFieldByEntity(entity);
		if(list==null||list.size()==0){
			return "";
		}
		buildSqlTableName(entity);
		StringBuffer sql = new StringBuffer("insert into ");
		sql.append(sqlTableName+"( ");
		StringBuffer rowStr = new StringBuffer();
		for(String s : list){
			rowStr.append(s);
		}
		String sqlStr = "insert into process(id,name,processKey)values(:id,:name,:processKey)";
		return sqlStr;
	}
	
}
